import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { nanoid } from 'nanoid'
import './index.css'

export default class Hello extends Component {

    // 对接收的props进行类型、必要性的限制
    static propTypes = {
        addNewTodo: PropTypes.func.isRequired
    }

    handlerKeyUp = (e) => {
        if (!e.target.value.trim() || e.key !== 'Enter') return
        this.props.addNewTodo({
            id: nanoid(),
            name: e.target.value,
            done: false
        })
        e.target.value = ""
    }

    render() {
        return (
            <div className="header">
                <input onKeyUp={this.handlerKeyUp} type="text" placeholder="请输入任务名称" />
            </div>
        )
    }
}